package bosc.cn.controller;

import bosc.cn.api.PaymentApi;
import bosc.cn.converter.payment.PaymentConfirmReqConverter;
import bosc.cn.converter.payment.PaymentConfirmResConverter;

import bosc.cn.dto.common.CommonResponse;
import bosc.cn.dto.common.RequestWrapper;
import bosc.cn.dto.common.ResponseWrapper;
import bosc.cn.dto.payment.PaymentConfirmReqDTO;
import bosc.cn.dto.payment.PaymentConfirmResDTO;
import bosc.cn.service.payment.PaymentConfirmService;
import bosc.cn.service.payment.bo.PaymentConfirmReqBO;
import bosc.cn.service.payment.bo.PaymentConfirmResBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/bank")
public class PaymentController implements PaymentApi {

    @Autowired
    private PaymentConfirmService paymentConfirmService;

    @Override
    @PostMapping("/pay")
    public ResponseWrapper<PaymentConfirmResDTO> confirmPayment(
            @RequestBody RequestWrapper<PaymentConfirmReqDTO> reqWrapper) {

        // 获取 body
        PaymentConfirmReqDTO body = reqWrapper.getBody();

        // DTO -> BO
        PaymentConfirmReqBO reqBO = PaymentConfirmReqConverter.toBO(body);

        // 调用 Service，返回的是 ResponseWrapper<PaymentConfirmResBO>
        ResponseWrapper<PaymentConfirmResBO> resWrapper = paymentConfirmService.confirmPayment(reqBO);

        // 从 Service 返回结果中取出 head 和 body
        CommonResponse head = resWrapper.getHead();
        PaymentConfirmResBO resBO = resWrapper.getBody();

        // BO -> DTO
        PaymentConfirmResDTO resDTO = PaymentConfirmResConverter.toDTO(resBO);

        // 封装最终响应（head 原样传递，body 转换成 DTO）
        ResponseWrapper<PaymentConfirmResDTO> responseWrapper = new ResponseWrapper<>();
        responseWrapper.setHead(head);
        responseWrapper.setBody(resDTO);

        return responseWrapper;
    }


}
